package com.tarim.streetdance.api;

import com.alibaba.fastjson.JSONObject;
import com.tarim.streetdance.model.User;
import com.tarim.streetdance.service.UserService;
import com.tarim.streetdance.util.Constants;
import com.tarim.streetdance.util.JsonResult;
import com.tarim.streetdance.util.RedisUtil;
import com.tarim.streetdance.vo.Criteria;
import com.tarim.streetdance.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@CrossOrigin
@RequestMapping("/api/adminUser")
public class AdminUserApi {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 管理员 查询用户列表
     * @param request
     * @param pageIndex
     * @param pageSize
     * @param queryCondition
     * @return
     */
    @RequestMapping(value = "/selectUserByCondition")
    public JsonResult selectUser (HttpServletRequest request,
                                    @RequestParam(value = "pageIndex", defaultValue = "0") Integer pageIndex,
                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                    @RequestParam(value = "queryCondition", defaultValue = "") String queryCondition){
        JsonResult js;
        try {
            Object object = redisUtil.get(request.getHeader("token"));
            if (object == null) {
                js = new JsonResult(Constants.STATUS_FAIL, "登录后查看数据");
                return js;
            }
            JSONObject jsonObject = (JSONObject) object;
            User user = jsonObject.toJavaObject(User.class);
            if (user.getUserType() != 0) {
                js = new JsonResult(Constants.STATUS_FAIL, "非管理员用户没有权限访问");
                return js;
            }
            PageBean<User> pageBean = new PageBean<>(pageIndex, pageSize);
            Criteria criteria = new Criteria();
            criteria.addExtField("queryCondition", queryCondition);
            pageBean = userService.selectByQueryCondition(criteria, pageBean);
            js = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", pageBean);
        }catch (Exception ex) {
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "系统异常");
        }
        return js;
    }

    /**
     * 管理员 操作用户
     * @param request
     * @param user
     * @return
     */
    @RequestMapping(value = "/updateUserById", method = RequestMethod.POST)
    public JsonResult updateUser (HttpServletRequest request, User user) {
        JsonResult js;
        try {
            Object object = redisUtil.get(request.getHeader("token"));
            if (object == null) {
                js = new JsonResult(Constants.STATUS_FAIL, "登录后可进行操作");
                return js;
            }
            JSONObject jsonObject = (JSONObject) object;
            User record = jsonObject.toJavaObject(User.class);
            if (record.getUserType() != 0) {
                js = new JsonResult(Constants.STATUS_FAIL, "非管理员用户没有权限访问");
                return js;
            }
            user.setModifyTime(new Date());
            int i = userService.updateByPrimaryKeySelective(user);
            if (i != 0) {
                js = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
            }else {
                js = new JsonResult(Constants.STATUS_FAIL, "操作失败");
            }
        }catch (Exception ex) {
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "系统异常");
        }
        return js;
    }
}
